package objects 
{
	import flash.geom.Point;
	import starling.display.Image;
	import starling.display.Sprite;
	import objects.MyObject;
	import starling.events.Event;
	import starling.utils.deg2rad;
	import starling.utils.rad2deg;
	/**
	 * ...
	 * @author Tuan
	 */
	public class Bullet extends MyObject 
	{
		private var m_layer : Sprite;
		
		public var m_maxTimeLive : Number;
		public var m_timeLive : Number;
		public var m_isTarget : Boolean;
		public var m_Active : Boolean;
		public var m_bulletSprite : Image;
		public var temp : Point;
		public var m_arrow : Image;
		public var m_angleInRad : Number;
		private var m_percentDeso:Number; //get from database - su dung lam giam ty le anh huong cua armor dich 
		private var m_percentDouble:Number // get from database - ty le nay luc dau = 0, khi nang cap skill se tang len, su dung de lam double damage
		public function Bullet(_sprite : Sprite, _pos : Point, t : Number, max_V : Number, _damage : Number, level : int, percentdouble : Number, deso : Number ) 
		{
			m_percentDouble = percentdouble;
			m_percentDeso = deso;
			super();
			
			m_level = level;
			m_layer = _sprite; //layer
			m_pos = _pos;
			
			m_timeLive = 0;
			m_maxTimeLive = t + 0.1;
			m_damage = _damage;
			m_minDamage = 1;
			m_maxDamage = 1;
			
			if (m_level == 1)
			{
				m_minDamage = 4;
				m_maxDamage = 6;
			}
			else if (m_level == 2)
			{
				m_minDamage = 7;
				m_maxDamage = 11;
			}
			else if (m_level == 3)
			{
				m_minDamage = 10;
				m_maxDamage = 16;
			}
			
			m_damage = randomRange(m_minDamage, m_minDamage);
			
			
		    m_isTarget = false;
			m_Active = false;
			m_maxVelocity = max_V;
			m_collision = false;
			m_velocity =  new Point(0, 0);
			temp = new Point(0, 0);
			m_choosedEnemy = null;
			m_angleInRad = 0;
			m_percentDamage = 1;
			
			this.addEventListener(Event.ADDED_TO_STAGE, init);
			
			m_layer.addChild(this);
		}
		private function init(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, init);
		    m_bulletSprite = new Image(Assets.getTexture("bullet"));
			
/*			this.setAnchorPoint();
			this.setPos(m_pos);
			
			this.addChild(m_bulletSprite);*/
			
		    m_arrow = new Image (Assets.getTexture ("arrow"));
		    m_arrow.pivotX = m_arrow.width / 2 ;
		    m_arrow.pivotY = m_arrow.height / 2 ;
			
		    m_arrow.scaleX = 0.5;
			m_arrow.scaleY = 0.5;
			
			m_arrow.x = m_pos.x;
			m_arrow.y = m_pos.y ;
			
            
			this.addChild (m_arrow);
			
		}
		private function setAnchorPoint():void {
			m_bulletSprite.pivotX = m_bulletSprite.width / 2 ;
			m_bulletSprite.pivotY = m_bulletSprite.height / 2 ;
		}
		private function setPos ( _pos : Point) : void
		{
			m_bulletSprite.x = _pos.x;
			m_bulletSprite.y = _pos.y;
		}
		private function setScale ( _scale : Number) :void
		{
			m_bulletSprite.scaleX = _scale;
			m_bulletSprite.scaleY = _scale ;
		}
		
		public function setTarget (_target : Point, enemy : MyObject) :void
		{
			if ( m_isTarget == false)
			{
				m_choosedEnemy = enemy;
				m_targetPos =  _target;
				m_isTarget = true;
				
				
				m_angleInRad = Math.atan2 (m_targetPos.y - m_pos.y, m_targetPos.x - m_pos.x) * 180/Math.PI; //get degree
				m_arrow.rotation = deg2rad(m_angleInRad); //deg 2 rad
			}
		}
		override public function release() : void {
			this.removeChild(m_arrow);
			removeChildren();
		}
		
	    override public function update(dt:Number):void
		{
			
			if(m_choosedEnemy!=null && m_choosedEnemy.m_hp <=0 ) //target chet'
	        {
		       m_choosedEnemy = null;
		       m_isTarget = false ;
	        }
			
		    if ( m_Active == true)
			{
				m_timeLive += dt;
				if ( m_timeLive >= m_maxTimeLive)
				{
					m_timeLive = 0;
					m_collision = true;
				}
				
				m_velocity = ccpNormalize ( m_targetPos.subtract(m_pos));
				m_velocity = ccpMult (m_velocity, m_maxVelocity);
				m_pos = m_pos.add(m_velocity);
				m_arrow.x = m_pos.x ;
				m_arrow.y = m_pos.y ;
			}
		}
		public function findAngle ( pt1 :Point, pt2 : Point) : Number
		{
			var angle : Number = Math.atan2 ( pt1.y - pt2.y , pt1.x - pt2.x ) * ( 180 / Math.PI);
			angle = angle < 0 ? angle + 360 : angle;
			return angle ;
		}
		override public function Collision ( _enemy : MyObject, dt : Number) :void
		{
			if ( m_choosedEnemy != null)
			{
			  if (m_choosedEnemy != null && m_arrow.bounds.intersects( m_choosedEnemy.getRect()) == true )
			  {
				  m_collision = true;
				  if (m_choosedEnemy.m_armor == Global.ARMOR_NONE)
				  {
					  if (m_percentDouble > 0 && randomRange(1, 5) ==1)
					  {
						  this.m_damage *= 2;
					  }
				      m_choosedEnemy.m_hp -= this.m_damage;
					  
				  }
				  else if (m_choosedEnemy.m_armor == Global.ARMOR_LOW)
				  {
					    m_percentDamage = randomRange (1, 20 - m_percentDeso);
					    this.m_damage = this.m_damage - this.m_damage * m_percentDamage / 100;
					    if (m_percentDouble > 0 && randomRange(1, 5) ==1)
					    {
						  this.m_damage *= 2;
					    }
						
					    m_choosedEnemy.m_hp -= this.m_damage ;
				  }
				  else if (m_choosedEnemy.m_armor == Global.ARMOR_MEDIUM)
				  {
					    m_percentDamage = randomRange (21 - m_percentDeso, 50 - m_percentDeso);
					    this.m_damage = this.m_damage - this.m_damage * m_percentDamage / 100;
						 if (m_percentDouble > 0 && randomRange(1, 5) ==1)
					     {
						  this.m_damage *= 2;
					     }
					    m_choosedEnemy.m_hp -= this.m_damage ;
				  }
				  else if (m_choosedEnemy.m_armor == Global.ARMOR_HIGH)
				  {
					    m_percentDamage = randomRange (51 - m_percentDeso, 74 - m_percentDeso);
					    this.m_damage = this.m_damage - this.m_damage * m_percentDamage / 100;
						 if (m_percentDouble > 0 && randomRange(1, 5) ==1)
					     {
						  this.m_damage *= 2;
					     }
					    m_choosedEnemy.m_hp -= this.m_damage ;
				  }
				  else if (m_choosedEnemy.m_armor == Global.ARMOR_HIGH)
				  {
					    m_percentDamage = randomRange (75- m_percentDeso, 95- m_percentDeso);
					    this.m_damage = this.m_damage - this.m_damage * m_percentDamage / 100;
						
						 if (m_percentDouble > 0 && randomRange(1, 5) ==1)
					     {
						  this.m_damage *= 2;
					     }
					    m_choosedEnemy.m_hp -= this.m_damage ;
				  }
			  }
			}
		}
	}

}